<?php
/**
* Email Controller. Contains all Chapter 6 examples.
*
* @author <Your Name Here>, <Your Email Address>
* @package Beginning_Zend_Framework
*/

class EmailController extends Zend_Controller_Action
{

    public function init()
    {
    /* Initialize action controller here */
    }

    public function indexAction()
    {
    // action body
    }

    /**
    * Basic example using default settings.
    *
    */
    public function sendMailAction()
    {
        //Send out the welcome message
        $MailObj = new Zend_Mail();
        $emailMessage = "Hola mundo ZEND .... ahi vamos";
        $fromEmail = "dcubas16@hotmail.com";
        $fromFullName = "Diego Antonio Nuez Cubas";
        $to= "dcubas16@gmail.com";
        $subject = "Hola Diego este es un Mensaje";
        $MailObj->setBodyText($emailMessage);
        $MailObj->setFrom($fromEmail, $fromFullName);
        $MailObj->addTo($to);
        $MailObj->setSubject($subject);
        try
        {
            $MailObj->send();
            echo "E-mail sent successfully!!!";
        }
        catch(Zend_Mail_Exception $e)
        {
             echo "SALIO MAL ".$e;
        //Your Error message here.
        }
        //Suppress the view.
        $this->_helper->viewRenderer->setNoRender();
    }

    /**
    * Send email using SMTP Host.
    *
    */
    public function smtpSendMail1Action()
    {
        //Create SMTP connection Object
        $configInfo = array('auth' => 'login',
        'ssl' => 'tls',
        'username' => 'dcubas16@gmail.com',
        'password' => 'depeche11',
        'port' => '587');
        $smtpHost = new Zend_Mail_Transport_Smtp('smtp.gmail.com',$configInfo);
        //Create Zend_Mail object.
        $MailObj = new Zend_Mail();
        //Initialize parameters.
        $emailMessage = "Hola esto es para enviar mensajes SMTP ... XD";
        $fromEmail = "dcubas16@ssmail.com";
        $fromFullName = "Diego Antonio NUnez Cubas";
        $to = "dcubas16@gmail.com";
        $subject = "Hola esto es para enviar mensajes SMTP";
        $MailObj->setBodyText($emailMessage);
        $MailObj->setFrom($fromEmail, $fromFullName);
        $MailObj->addTo($to);
        $MailObj->addCc('dcubas16@hotmail.com','Diego');
        $MailObj->addBcc('dcubas16@hotmail.com');

//        $MailObj->setDate("15/04/2040");

        $MailObj->setSubject($subject);
        //Send Email using transport protocol.
        try{
        $MailObj->send($smtpHost);
        echo "MENSAJE SMTP ENVIADO CON EXITO !!! ... xD";
        }catch(Zend_Mail_Exception $e){
        //Your error message here.
        echo "MENSAJE SMTP NO SE PUDO ENVIAR !!! ... :(".$e->getMessage();
            
        }
        //Suppress the view.
        $this->_helper->viewRenderer->setNoRender();
    }

    /**
    * Send email using SMTP Host.
    *
    */
    public function smtpSendMailAction()
    {
        //Create SMTP connection Object
        $configInfo = array('auth' => 'login',
        'ssl' => 'tls',
        'username' => 'dcubas16@gmail.com',
        'password' => 'depeche11',
        'port' => '587');
        $smtpHost = new Zend_Mail_Transport_Smtp('smtp.gmail.com',$configInfo);
        //Create Zend_Mail object.
        $MailObj = new Zend_Mail();
        //Initialize parameters.
        $emailMessage = "Hola esto es para enviar mensajes SMTP ... XD";
        $fromEmail = "dcubas16@gmail.com";
        $fromFullName = "Diego Antonio NUnez Cubas";
        $to = "dcubas16@hotmail.com";
        $subject = "Hola esto es para enviar mensajes SMTP";


        $validator = new Zend_Validate_EmailAddress( Zend_Validate_Hostname::ALLOW_DNS,true);
        if($validator->isValid($to))
        {
            $MailObj->setBodyText($emailMessage);
            $MailObj->setFrom($fromEmail, $fromFullName);
            $MailObj->addTo($to);
            $MailObj->setSubject($subject);

            //Send Email using transport protocol.
            try
            {
                $MailObj->send($smtpHost);
                echo "Email enviado correctamente";
            }
            catch(Zend_Mail_Exception $e)
            {
                //Your Error message here.
                echo $e->getMessage();
            }
        }
        else
        {
            //Messages in array.
            $messages = $validator->getMessages();
            foreach($messages as $message){echo $message.'<br/>';}
        }

        //Suppress the view.
        $this->_helper->viewRenderer->setNoRender();

    }

    /**
    * Send HTML based email.
    *
    */
    public function sendHtmlEmailAction()
    {
        //Create SMTP connection Object
        $configInfo = array('auth' => 'login',
        'ssl' => 'tls',
        'username' => 'dcubas16@gmail.com',
        'password' => 'depeche11',
        'port' => '587');
        $smtpHost = new Zend_Mail_Transport_Smtp('smtp.gmail.com',$configInfo);
        //Create Zend_Mail object.
        $MailObj = new Zend_Mail();
        $message= "<div style='background-color:red'><h1>Welcome to the example</h1><br>" .
                    "<p>An example email.</p></div>";
//        $message = "Hola esto es para enviar mensajes SMTP ... XD";
        //Initialize parameters.
        $emailMessage = "Hola esto es para enviar mensajes SMTP ... XD";
        $fromEmail = "dcubas16@ssmail.com";
        $fromFullName = "Diego Antonio NUnez Cubas";
        $to = "dcubas16@gmail.com";
        $subject = "Hola esto es para enviar mensajes SMTP";
        $MailObj->setBodyHtml($message);
        $MailObj->setFrom($fromEmail, $fromFullName);
        $MailObj->addTo($to);
        $MailObj->addCc('dcubas16@hotmail.com','Diego');
        $MailObj->addBcc('dcubas16@hotmail.com');

        $MailObj->setSubject($subject);
        //Send Email using transport protocol.
        try{
        $MailObj->send($smtpHost);
        echo "MENSAJE SMTP ENVIADO CON EXITO !!! ... xD";
        }catch(Zend_Mail_Exception $e){
        //Your error message here.
        echo "MENSAJE SMTP NO SE PUDO ENVIAR !!! ... :(".$e->getMessage();

        }
        //Suppress the view.
        $this->_helper->viewRenderer->setNoRender();
    }

    /**
    * Send out email with attachment
    *
    */
    public function sendEmailWithAttachmentAction()
    {
        //Create SMTP connection Object
        $configInfo = array('auth' => 'login',
        'ssl' => 'tls',
        'username' => 'dcubas16@gmail.com',
        'password' => 'depeche11',
        'port' => '587');
        $smtpHost = new Zend_Mail_Transport_Smtp('smtp.gmail.com',$configInfo);
        //Create Zend_Mail object.
        $MailObj = new Zend_Mail();
        $message= "<div style='background-color:red'><h1>Welcome to the example</h1><br>" .
                    "<p>An example email.</p></div>";

        //Read image data.
        $fileLocation = '../application/123.jpg';
        //Check if the file exists and is readable
        if(!$fileHandler = fopen($fileLocation, 'rb'))
        {
            throw new Exception("The file could not be
            found or is not readable.");
        }
        $fileContent = fread($fileHandler, filesize($fileLocation));
        fflush($fileHandler);
        fclose($fileHandler);

        //Initialize parameters.
        $emailMessage = "Hola esto es para enviar mensajes SMTP ... XD";
        $fromEmail = "dcubas16@gmail.com";
        $fromFullName = "Diego Antonio NUnez Cubas";
        $to = "dcubas16@hotmail.com";
        $subject = "Hola esto es para enviar mensajes SMTP";
        $MailObj->setBodyHtml($message);
        $MailObj->setFrom($fromEmail, $fromFullName);
        $MailObj->addTo($to);
        $MailObj->addCc('dcubas16@hotmail.com','Diego');
        $MailObj->addBcc('dcubas16@hotmail.com');

        $MailObj->setSubject($subject);
        //Send Email using transport protocol.
        $MailObj->createAttachment($fileContent,'image/jpeg', Zend_Mime::DISPOSITION_ATTACHMENT,Zend_Mime::ENCODING_BASE64);

        try{
        $MailObj->send($smtpHost);
        echo "MENSAJE SMTP ENVIADO CON EXITO !!! ... xD";
        }catch(Zend_Mail_Exception $e){
        //Your error message here.
        echo "MENSAJE SMTP NO SE PUDO ENVIAR !!! ... :(".$e->getMessage();

        }
        //Suppress the view.
        $this->_helper->viewRenderer->setNoRender();
    }


    /**
    * Send out email with attachment
    *
    */
//    public function sendEmailWithAttachmentAction()
//    {
//               //Create SMTP connection Object
//        $configInfo = array('auth' => 'login',
//        'ssl' => 'tls',
//        'username' => 'dcubas16@gmail.com',
//        'password' => 'incubas16',
//        'port' => '587');
//        $smtpHost = new Zend_Mail_Transport_Smtp('smtp.gmail.com',$configInfo);
//        //Create Zend_Mail object.
//        $MailObj = new Zend_Mail();
//        $message = "<h1>Welcome to the example</h1>".
//        "<br><p>An example email.</p>";
//        //Read image data.
//        $fileLocation = '/home/diego/Imagenes/23668_10150119327770541_812970540_11422770_5545706_n.jpg';
//        //Check if the file exists and is readable
//        if(!$fileHandler = fopen($fileLocation, 'rb'))
//        {
//            throw new Exception("The file could not be found or is not readable.");
//        }
//        $fileContent = fread($fileHandler, filesize($fileLocation));
//        fflush($fileHandler);
//        fclose($fileHandler);
//        //Initialize parameters.
//         $fromEmail = "dcubas16@ssmail.com";
//        $fromFullName = "Diego Antonio NUnez Cubas";
//        $to = "dcubas16@gmail.com";
//        $subject = "Hola esto es para enviar mensajes SMTP";
//        $MailObj->setBodyHtml($message);
//        $MailObj->setFrom($fromEmail, $fromFullName);
//        $MailObj->addTo($to);
//        $MailObj->addCc('dcubas16@hotmail.com','Diego');
//        $MailObj->addBcc('dcubas16@hotmail.com');
//
//        $MailObj->createAttachment($fileContent,'image/pjpeg',
//        Zend_Mime::DISPOSITION_ATTACHMENT);
//        //Send Email using transport protocol.
//        try{
//        $MailObj->send($smtpHost);
//        echo "Email sent successfully";
//        }catch(Zend_Mail_Exception $e){
//        //Your Error message here.
//        echo $e->getMessage();
//        }
//        //Suppress the view.
//        $this->_helper->viewRenderer->setNoRender();
//    }


}

